﻿using CPPEI.Coolzon.Designtime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.XProperty.Editors
{
    /// <summary>
    /// Interaction logic for ImageBrushEditWindow.xaml
    /// </summary>
    public partial class ImageSourceFileEditWindow : Window
    {
        public ImageSourceFile ReturnImageSourceFile { get; private set; }
        public ImageSourceFileEditWindow(ImageSourceFile imageSourceFile)
        {
            InitializeComponent();
            ReturnImageSourceFile = imageSourceFile;
        }
        private void btnSelectImage_Click(object sender, RoutedEventArgs e)
        {
            if(ReturnImageSourceFile == null)
            {
                CustomMessageBox.ShowWarning("请选择图片");
                return;
            }
            DialogResult = true;
        }

        private void cbStretch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            imagePreview.Stretch = (Stretch)cbStretch.SelectedValue;
        }

        private void _ctlBtnOK_Click_1(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

        private void cbPicName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbPicName.SelectedItem != null)
            {
                ImageRes imageRes = cbPicName.SelectedItem as ImageRes;

                FileInfo file = new FileInfo(imageRes.FullPath);

                BitmapImage image = null;
                using (FileStream fileStream = File.Open(file.FullName, FileMode.Open, FileAccess.Read))
                {
                    using (BinaryReader binReader = new BinaryReader(fileStream))
                    {
                        byte[] bytes = binReader.ReadBytes((int)file.Length);

                        image = new BitmapImage();
                        image.CacheOption = BitmapCacheOption.OnLoad;
                        image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                        image.BeginInit();
                        image.StreamSource = new MemoryStream(bytes);
                        image.EndInit();
                    }
                }

                ReturnImageSourceFile = new ImageSourceFile(image, imageRes.FullPath);

                imagePreview.Source = ReturnImageSourceFile.ImageSource;
                imagePreview.Height = image.Height;
                imagePreview.Width = image.Width;
            }
            else
            {
                ReturnImageSourceFile = null;
                imagePreview.Source = null;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Uri fullPath = null;
            if (ReturnImageSourceFile != null)
            {
                BitmapImage bitmapImage = (BitmapImage)ReturnImageSourceFile.ImageSource;
                fullPath = new Uri(ReturnImageSourceFile.Path);
            }
            ImageRes selected = null;
            ImageRes[] imageList = ResPoolFactory.GetImageResPool().GetImages();
            if (imageList != null && imageList.Length > 0)
            {
                foreach(var item in imageList)
                {
                    cbPicName.Items.Add(item);
                    if(fullPath != null && fullPath == new Uri(item.FullPath))
                    {
                        selected = item;
                    }
                }
                if(selected != null)
                {
                    cbPicName.SelectedItem = selected;
                }
            }
        }

        private void _ctlbtnSetNull_Click(object sender, RoutedEventArgs e)
        {
            cbPicName.SelectedItem = null;
            ReturnImageSourceFile = null;
            imagePreview.Source = null;
        }
    }
}
